////////////////////////////////////////////////////////////////////////////////
// Filename: testgame.cpp
////////////////////////////////////////////////////////////////////////////////
#include "testgame.hpp"
#include "core/graphics/sprite.hpp"
#include "core/graphics/shaders/positiontextureshader.hpp"

TestGame::TestGame(LPCSTR applicationName) : BasicGame(applicationName) {
	poly = NULL;
//	rect = NULL;
}

TestGame::~TestGame() {

}

void TestGame::Initialize(GameObject* gameObject, Graphics* graphics) {
	m_vertices = new Vector2D*[5];
	unsigned long* indices = new unsigned long[9];

	m_vertices[0] = new Vector2D(-100.0f, -100.0f);
	m_vertices[1] = new Vector2D(-150.0f, 0.0f);
	m_vertices[2] = new Vector2D(0.0f, 100.0f);
	m_vertices[3] = new Vector2D(150.0f, 0.0f);
	m_vertices[4] = new Vector2D(100.0f, -100.0f);

	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;
	indices[3] = 0;
	indices[4] = 2;
	indices[5] = 3;
	indices[6] = 0;
	indices[7] = 3;
	indices[8] = 4;

	poly = new miv::Polygon();
	poly->Initialize(m_vertices, indices, 5, 9,miv::Color::Red(), graphics->GetDirect3D()->GetDevice());

	rect = new miv::Rectangle();
	rect2 = new miv::Rectangle();
	Vector2D* pos = new Vector2D(200, 250);
	rect->Initialize(400, 200, pos, miv::Color::Green(), graphics->GetDirect3D()->GetDevice());
	pos = new Vector2D(-300, -200);
	rect2->Initialize(400, 400, pos, miv::Color::Blue(), graphics->GetDirect3D()->GetDevice());
	circle = new miv::Circle();
	pos = new Vector2D(200, 250);
	circle->Initialize(100, 32, pos, miv::Color::Yellow(), graphics->GetDirect3D()->GetDevice());
	circle2 = new miv::Circle();
	pos = new Vector2D(-300, -200);
	circle2->Initialize(150, 64, pos, miv::Color::Olive(), graphics->GetDirect3D()->GetDevice());
	pos = new Vector2D(300, -200);
	sprite = new Sprite();
	sprite->Initialize(pos, graphics, L"../Mark-IV-Engine/trunk/rsc/texture/alpha_test.dds", true);
	pos = new Vector2D(-400, 200);
	sprite2 = new Sprite();
	sprite2->Initialize(pos, graphics, L"../Mark-IV-Engine/trunk/rsc/texture/applejack.dds", true);
	delete pos;
}

void TestGame::Shutdown(GameObject* gameObject, Graphics* graphics) {
	if (m_vertices) {
		delete[] m_vertices;
		m_vertices = NULL;
	}
	if (poly) {
		poly->Shutdown();
		delete poly;
		poly = NULL;
	}
	if (rect) {
		rect->Shutdown();
		delete rect;
		rect = NULL;
	}
	if (rect2) {
		rect2->Shutdown();
		delete rect2;
		rect2 = NULL;
	}
	if (circle) {
		circle->Shutdown();
		delete circle;
		circle = NULL;
	}
	if (circle2) {
		circle2->Shutdown();
		delete circle2;
		circle2 = NULL;
	}
	if (sprite) {
		sprite->Shutdown();
		delete sprite;
		sprite = NULL;
	}
	if (sprite2) {
		sprite2->Shutdown();
		delete sprite2;
		sprite2 = NULL;
	}
}

void TestGame::Update(GameObject* gameObject, Graphics* graphics) {
	if (gameObject->GetInput()->IsKeyDown(VK_ESCAPE)) {
		PostQuitMessage(0);
	}
	if (gameObject->GetInput()->IsKeyDown(VK_RIGHT)) {
		sprite2->GetPosition()->SetX(sprite2->GetPosition()->GetX() + 10.0f);
		graphics->TranslateX(sprite2->GetModelMatrix(), 10.0f);
	}
	if (gameObject->GetInput()->IsKeyDown(VK_LEFT)) {
		sprite2->GetPosition()->SetX(sprite2->GetPosition()->GetX() - 10.0f);
		graphics->TranslateX(sprite2->GetModelMatrix(), -10.0f);
	}
	if (gameObject->GetInput()->IsKeyDown(VK_UP)) {
		sprite2->GetPosition()->SetY(sprite2->GetPosition()->GetY() + 10.0f);
		graphics->TranslateY(sprite2->GetModelMatrix(), 10.0f);
	}
	if (gameObject->GetInput()->IsKeyDown(VK_DOWN)) {
		sprite2->GetPosition()->SetY(sprite2->GetPosition()->GetY() - 10.0f);
		graphics->TranslateY(sprite2->GetModelMatrix(), -10.0f);
	}

	graphics->RotateZ(poly->GetModelMatrix(), -0.01f);
}

void TestGame::Render(GameObject* gameObject, Graphics* graphics) {
	sprite->Draw(graphics);
	rect->Draw(graphics);
	rect2->Draw(graphics);
	circle->Draw(graphics);
	circle2->Draw(graphics);
	poly->Draw(graphics);
	sprite2->Draw(graphics);
}
